<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class homeController extends Controller {

    public function __construct($model, $action) {
        parent::__construct($model, $action);
        $this->_setModel('home');
    }

    public function index() {
        // Start Caching with PHP Memcache
        $m = new Memcache();
        $m->connect("127.0.0.1", 11211) or die("Cannot connect to Memcached Server");
        
        // Home model
        $product = new product();
		$homeHTML = $m->get("home");
		//$m->delete('home'); //mở dòng này ra lúc test
        // Not exist new_product in memcached server
        if (!$m->get("home")) 
		{	
		$content="";	
		$category = new category();
		$listcategory= $category->getAllCate();
		$content .='<html>
    <head>
        <title>Ung dung dau gia</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       
        <link rel="stylesheet" type="text/css" href="views/style/style.css" />
        <script src="views/js/jquery/jquery-1.8.3.js" ></script>
        <script src="views/js/jquery/jquery-1.8.3.min.js" ></script>
    </head>
	<body>
        <div id="wrapper_homepage">
		<script>
            $(document).ready(function(){
                $("#linkTimKiem").click(function(){
                    $("#boxSearch").toggle();
                });
            });
        </script>
 
 		
        <div id="wrapper_header">
            <img id="imgHome" src="views/images/Home-64.png" alt="Homepage" />
            <select id="selCategory">
                <option value="-1">chọn tất cả</option>';
				foreach ($listcategory as $item)
				{
                 	$content .= '<option value='.$item['idCategory'].'>'.$item['name'].'</option>';
             
				}
            
			$content .= '</select>
            <a id="linkTimKiem">Tìm kiếm</a>
            <!--Box tim kiem-->
            <form id="boxSearch">
                <div>
                    Tên vật phẩm 
                    <input type="text" name="txtTenVP" id="txtTenVP" size="17"/>
                </div>
                <p>
                    <label>The loai</label>
                    <select id="SearchCategory">
                        <option value="-1">chọn tất cả</option>';
				foreach ($listcategory as $item)
				{
                 	$content .= '<option value='.$item['idCategory'].'>'.$item['name'].'</option>';
             
				}
            
			$content .= '
                    </select>
                </p>
                <p>
                    <label>Ngày bắt đầu</label>
                     <select class="DateStarEnd">
                         
                     </select>
                </p>
                <p>
                     <select class="DateStarEnd">
                         
                     </select>
                </p>
                <p>
                    <label>Ngày kết thúc</label>
                     <select class="DateStarEnd">
                         
                     </select>
                </p>
                <p>
                     <select class="DateStarEnd">
                         
                     </select>
                </p>
            </form></div>';
	
		
		$category->getAll();
        $listNewProduct = $product->getNewProduct();
		$content .= "<div class='new_product'>";
		$content .= "<div class='title_new_product'>";
		$content .= "SẢN PHẨM MỚI";
		$content .= "</div>";
		$content .= "<div class='list_new_product'>";
			
		foreach ($listNewProduct as $item) {
			$content .= "<div class='products_list'>";
			$content .= "<a href='#' class='link_new_name'>";
			$content .= "<img src='views/images/ex_product.gif' class='img_new_product'/>";
			$content .= "<div class='new_name'>";
			$content .= $item['Name'];
			$content .= "</div>";
			$content .= "</a>";
			$content .= "</div>"; 
		}
		$content .= "</div>"; // 
		$content .= "</div>"; // div new_product
		$listOldProduct = $product->getOldProduct();
		
		$content .= ' <div id="left_contain">
                <div class="title_hp">
                    <span class="title_content">Sản phẩm gần hết hạn</span>
                </div>';
		
		foreach ($listOldProduct as $item) {
			$content .=' <a href="#" class="link_product">
                    <div class="body_hp">
                        <img class="img_product" src="views/images/ex_product.gif" alt="sp" />
                        <p class="title_product">'.$item['Name'].'</p>
                        <p class="date_end_product">Ngày kết thúc: '.$item['timeEnd'].'</p>
                        <div class="clear_float"></div>
                    </div>
                </a>';
		}		
        $content .='</div>'; //div left_contain
		
		$content .='  <div id="right_contain">
                <div class="title_hp">
                    <span class="title_content">Sản phẩm hot</span>
                </div>';
		$listHotProduct = $product->getHotProduct();
		foreach ($listHotProduct as $item) {
			
            $content .='    <a href="#" class="link_product">
                    <div class="body_hp">
                        <img class="img_product" src="views/images/ex_product.gif" alt="sp" />
                        <p class="title_product">'.$item['Name'].'</p>
                        <p class="date_end_product">Ngày kết thúc:'.$item['timeEnd'].'</p>
                        <div class="clear_float"></div>
                    </div>
                </a>';
		}
		$content .='</div>'; //div right_contain
		
		$content .= '</div>'; //div wrapper_homepage
		$content .= "</body>";
		$content .= "</html>";
            // Save to memcached
        $m->set("home", $content);
        $this->_setView("index");
        $this->_view->set("content", $content);
        return $this->_view->output();
		}
		else {
		
		$content = $m->get('home');
		$this->_setView("index");
        $this->_view->set("content", $content);
        return $this->_view->output();
		}
	}
}

?>
